package com.iraeta.service.page;

import cn.hutool.core.util.StrUtil;
import com.iraeta.dao.main.*;
import com.iraeta.entity.main.*;
import com.iraeta.uitl.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class SalServiceImpl {
    @Autowired
    BpmSalxsddcDao bpmSalxsddcDao;
    @Autowired
    ErpSomainDao erpSomainDao;

    @Autowired
    ErpRdrecordScDao erpRdrecordScDao;
    @Autowired
    ErpMomOrderDetailDao erpMomOrderDetailDao;
    @Autowired
    ErpRdrecord32Dao erpRdrecord32Dao;
    @Autowired
    ErpRdrecord01WxDao erpRdrecord01WxDao;
    @Autowired
    ErpRdrecord11Dao erpRdrecord11Dao;

    @Autowired
    ErpgqSomainDao erpgqSomainDao;
    @Autowired
    ErpgqRdrecord32Dao erpgqRdrecord32Dao;
    @Autowired
    ErpgqCurrentstockDao erpgqCurrentstockDao;

    /*
    * csocode_s:根据销售订单查询条件
    * */
    public List<HashMap<String, Object>> listSoMain(String csocode_s, String cinvname_s, String ccusname_s){
        List<HashMap<String, Object>> res = erpSomainDao.searchSoMain(csocode_s, cinvname_s, ccusname_s);
        for(HashMap<String, Object> xsddcMap : res){
            String csocode = Util.parseStringFromMap(xsddcMap, "cSoCode");      //销售订单号
            String cchbm = Util.parseStringFromMap(xsddcMap, "cInvCode");       //存货编码
            if(StrUtil.isEmpty(cchbm) || StrUtil.isEmpty(csocode) ){
               continue;
            }

            String cchbm_d = cchbm + "D";
            String cchbm_j = cchbm + "j";
            BigDecimal dCount = new BigDecimal("0");Set<String> whNameSet_d = new HashSet<String>();HashMap<String, BigDecimal> dCountMap = new HashMap<String, BigDecimal>();
            BigDecimal jCount = new BigDecimal("0");Set<String> whNameSet_j = new HashSet<String>();HashMap<String, BigDecimal> jCountMap = new HashMap<String, BigDecimal>();
            BigDecimal count = new BigDecimal("0");Set<String> whNameSet = new HashSet<String>();HashMap<String, BigDecimal> countMap = new HashMap<String, BigDecimal>();
            /*根据销售订单和物料编码 获取所有的生产订单号， 可能有多个*/
            List<ErpMomOrderDetail> erpMomOrderDetailList = erpMomOrderDetailDao.selectBySocodeAndInvCode(csocode, cchbm);
            if(erpMomOrderDetailList!=null && erpMomOrderDetailList.size() > 0){
                Set<String> mocodeSet = new HashSet<>();
                for (ErpMomOrderDetail erpMomOrderDetail : erpMomOrderDetailList) {
                    mocodeSet.add(erpMomOrderDetail.getMocode());
                }
                //根据物料编码和生产订单号，查询产成品入库， 统计入库数量
                for (String mocode : mocodeSet) {
                    List<ErpRdrecordSc>  erpRdrecordScList_d = erpRdrecordScDao.selectByCinvcode(cchbm_d, mocode);
                    for (ErpRdrecordSc erpRdrecordSc : erpRdrecordScList_d) {
                        dCount = dCount.add(erpRdrecordSc.getIquantity());
                        String cwhName = Util.getCqName( erpRdrecordSc.getCwhname());
                        whNameSet_d.add(cwhName);
                        if(dCountMap.containsKey(cwhName)){
                            dCountMap.put(cwhName, dCountMap.get(cwhName).add(erpRdrecordSc.getIquantity()));
                        }else{
                            dCountMap.put(cwhName, erpRdrecordSc.getIquantity());
                        }
                    }
                    List<ErpRdrecordSc>  erpRdrecordScList_j = erpRdrecordScDao.selectByCinvcode(cchbm_j, mocode);
                    for (ErpRdrecordSc erpRdrecordSc : erpRdrecordScList_j) {
                        jCount = jCount.add(erpRdrecordSc.getIquantity());
                        String cwhName = Util.getCqName( erpRdrecordSc.getCwhname());
                        whNameSet_j.add(cwhName);
                        if(jCountMap.containsKey(cwhName)){
                            jCountMap.put(cwhName, jCountMap.get(cwhName).add(erpRdrecordSc.getIquantity()));
                        }else{
                            jCountMap.put(cwhName, erpRdrecordSc.getIquantity());
                        }
                    }
                    List<ErpRdrecordSc>  erpRdrecordScList = erpRdrecordScDao.selectByCinvcode(cchbm, mocode);
                    for (ErpRdrecordSc erpRdrecordSc : erpRdrecordScList) {
                        count = count.add(erpRdrecordSc.getIquantity());
                        String cwhName = Util.getCqName( erpRdrecordSc.getCwhname());
                        whNameSet.add(cwhName);
                        if(countMap.containsKey(cwhName)){
                            countMap.put(cwhName, countMap.get(cwhName).add(erpRdrecordSc.getIquantity()));
                        }else{
                            countMap.put(cwhName, erpRdrecordSc.getIquantity());
                        }
                    }
                }
            }
            /*根据销售订单 获取外协入库数据*/
            List<ErpRdrecord01Wx>  erpRdrecord01WxList_d = erpRdrecord01WxDao.selectByCinvcodeAndXsOrderId(cchbm_d, csocode);
            for (ErpRdrecord01Wx erpRdrecord01Wx : erpRdrecord01WxList_d) {
                dCount = dCount.add(erpRdrecord01Wx.getIquantity());
                String cwhName = Util.getCqName( erpRdrecord01Wx.getCwhname());
                whNameSet_d.add(cwhName);
                if(dCountMap.containsKey(cwhName)){
                    dCountMap.put(cwhName, dCountMap.get(cwhName).add(erpRdrecord01Wx.getIquantity()));
                }else{
                    dCountMap.put(cwhName, erpRdrecord01Wx.getIquantity());
                }
            }
            List<ErpRdrecord01Wx>  erpRdrecord01WxList_j = erpRdrecord01WxDao.selectByCinvcodeAndXsOrderId(cchbm_j, csocode);
            for (ErpRdrecord01Wx erpRdrecord01Wx : erpRdrecord01WxList_j) {
                jCount = jCount.add(erpRdrecord01Wx.getIquantity());
                String cwhName = Util.getCqName( erpRdrecord01Wx.getCwhname());
                whNameSet_j.add(cwhName);
                if(jCountMap.containsKey(cwhName)){
                    jCountMap.put(cwhName, jCountMap.get(cwhName).add(erpRdrecord01Wx.getIquantity()));
                }else{
                    jCountMap.put(cwhName, erpRdrecord01Wx.getIquantity());
                }
            }
            List<ErpRdrecord01Wx>  erpRdrecord01WxList = erpRdrecord01WxDao.selectByCinvcodeAndXsOrderId(cchbm, csocode);
            for (ErpRdrecord01Wx erpRdrecord01Wx : erpRdrecord01WxList) {
                count = count.add(erpRdrecord01Wx.getIquantity());
                String cwhName = Util.getCqName( erpRdrecord01Wx.getCwhname());
                whNameSet.add(cwhName);
                if(countMap.containsKey(cwhName)){
                    countMap.put(cwhName, countMap.get(cwhName).add(erpRdrecord01Wx.getIquantity()));
                }else{
                    countMap.put(cwhName, erpRdrecord01Wx.getIquantity());
                }
            }
            xsddcMap.put("dCount", dCount);
            xsddcMap.put("jCount", jCount);
            xsddcMap.put("count", count);
            xsddcMap.put("dwhName", Util.parseSetToString(whNameSet_d));
            xsddcMap.put("jwhName", Util.parseSetToString(whNameSet_j));
            xsddcMap.put("whName", Util.parseSetToString(whNameSet));
            xsddcMap.put("dwhMapStr", parseWhNameMap(dCountMap));
            xsddcMap.put("jwhMapStr", parseWhNameMap(jCountMap));
            xsddcMap.put("whMapStr", parseWhNameMap(countMap));

            /*根据材料出库信息查看下料数据*/
            BigDecimal xcount = new BigDecimal("0");Set<String> whNameSet_x = new HashSet<String>();HashMap<String, BigDecimal> xCountMap = new HashMap<String, BigDecimal>();
            List<ErpRdrecord11>  erpRdrecord11List = erpRdrecord11Dao.selectByInvcodeAndOrderCode(cchbm_d, csocode);
            for (ErpRdrecord11 erpRdrecord11 : erpRdrecord11List) {
                xcount = xcount.add(erpRdrecord11.getIquantity());
                String cwhName = Util.getCqName( erpRdrecord11.getCwhname());
                whNameSet_x.add(cwhName);
                if(xCountMap.containsKey(cwhName)){
                    xCountMap.put(cwhName, xCountMap.get(cwhName).add(erpRdrecord11.getIquantity()));
                }else{
                    xCountMap.put(cwhName, erpRdrecord11.getIquantity());
                }
            }
            xsddcMap.put("xcount", xcount);
            xsddcMap.put("xwhName", Util.parseSetToString(whNameSet_x));
            xsddcMap.put("xwhMapStr", parseWhNameMap(xCountMap));

            /*根据发货信息查看已发货数据*/
            BigDecimal fcount = new BigDecimal("0");;Set<String> whNameSet_f = new HashSet<String>();
            HashMap<String, BigDecimal> fCountMap = new HashMap<String, BigDecimal>();
            List<ErpRdrecord32>  erpRdrecord32List = erpRdrecord32Dao.selectByCinvcodeAndXsOrderId(cchbm, csocode);
            for (ErpRdrecord32 erpRdrecord32 : erpRdrecord32List) {
                fcount = fcount.add(erpRdrecord32.getIquantity());
                String cwhName = Util.getCqName( erpRdrecord32.getCwhname());
                whNameSet_f.add(cwhName);
                if(fCountMap.containsKey(cwhName)){
                    fCountMap.put(cwhName, fCountMap.get(cwhName).add(erpRdrecord32.getIquantity()));
                }else{
                    fCountMap.put(cwhName, erpRdrecord32.getIquantity());
                }
            }
            xsddcMap.put("fcount", fcount);
            xsddcMap.put("fwhName", Util.parseSetToString(whNameSet_f));
            xsddcMap.put("fwhMapStr", parseWhNameMap(fCountMap));
        }
        return res;
    }

    public List<HashMap<String, Object>> listGqSoMain(String csocode_s, String cinvname_s, String ccusname_s){
        List<HashMap<String, Object>> res = erpgqSomainDao.searchGqSoMain(csocode_s, cinvname_s, ccusname_s);
        for(HashMap<String, Object> xsddcMap : res){
            String csocode = Util.parseStringFromMap(xsddcMap, "cSoCode");      //销售订单号
            String cchbm = Util.parseStringFromMap(xsddcMap, "cInvCode");       //存货编码
            if(StrUtil.isEmpty(cchbm) || StrUtil.isEmpty(csocode) ){
                continue;
            }

            BigDecimal fcount = new BigDecimal("0");
            List<ErpgqRdrecord32>  erpgqRdrecord32List = erpgqRdrecord32Dao.selectByCinvcodeAndXsOrderId(cchbm, csocode);
            for (ErpgqRdrecord32 erpgqRdrecord32 : erpgqRdrecord32List) {
                fcount = fcount.add(erpgqRdrecord32.getIquantity());
            }
            xsddcMap.put("fcount", fcount);     //发货数量

            //查询库存和锁定数量
            ErpgqCurrentstock erpgqCurrentstock = erpgqCurrentstockDao.selectByPrimaryKey(cchbm);
            xsddcMap.put("kccount", erpgqCurrentstock.getIquantity());     //库存数量
            xsddcMap.put("lockcount", erpgqCurrentstock.getFstopquantity());     //锁定数量
        }
        return res;
    }

    private String parseWhNameMap(HashMap<String, BigDecimal> sourceMap){
        if(sourceMap == null || sourceMap.size() == 0){
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sourceMap.forEach((k,v) ->{
            String formatV = v.toString().replaceAll("0+$", "");
            if(formatV.endsWith(".")){
                formatV = formatV.substring(0, formatV.length() - 1);
            }
            sb.append(formatV).append("(").append(k).append("),");
        });
        String res = sb.toString();
        if(res.length() > 1){
            res = res.substring(0, res.length() - 1);
        }
        return res;
    }
}
